package com.example.imtoken.modules.main.wallet.details.trade.transfer

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.R
import com.example.imtoken.core.OnBottomSheetDialogFragmentListener
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.ActivityTransferBinding
import com.example.imtoken.modules.main.my.address.AddressActivity
import com.example.imtoken.modules.main.wallet.details.trade.transfer.set_price.SettingMinerFeeActivity
import com.example.imtoken.modules.scanning.ScanningActivity
import com.example.imtoken.view.transfer.TransferInfoDetailsFragment
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * 转账
 */
class TransferActivity :
    BaseActivity<TransferViewModel, ActivityTransferBinding>(TransferViewModel::class.java),
    OnBottomSheetDialogFragmentListener {

    private val title by lazy {
        intent.getStringExtra("title")
    }
    private val coin by lazy {
        intent.getStringExtra("coin")
    }

    private val contractAddress by lazy {
        intent.getStringExtra("contractAddress")
    }

    private val address by lazy {
        intent.getStringExtra("address")
    }

    private val amount by lazy {
        intent.getStringExtra("amount")
    }

    var data: String? = null

    override fun initLayoutId(): Int {
        return R.layout.activity_transfer
    }

    override fun init() {
        initBar(true, R.color.fragment_bg)
        dataBinding.viewModel = viewModel
        dataBinding.isGONE = coin != "trx"
        initTitle()
        initView()
        initClick()
        initObserve()
        if (coin != "trx") {
            viewModel.getGasInfo()
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initObserve() {
        viewModel.getTransferInfo().observe(this) {
            //开始转账
            val dialog = TransferInfoDetailsFragment(it)
            dialog.setOnBottomSheetDialogFragmentListener(this)
            dialog.show(supportFragmentManager, "dialog")
        }
        viewModel.getMinerFee().observe(this) {
            dataBinding.tvPrice.text = "$it ETH"
        }

        viewModel.address.observe(this) {
            viewModel._address = it
        }
        viewModel.amount.observe(this) {
            viewModel._amount = it
        }
        viewModel.isSubmit.observe(this) {
            if (it) {
                dataBinding.tvConfirm.setBackgroundResource(R.drawable.shape_create_btn_bg_true)
            } else {
                dataBinding.tvConfirm.setBackgroundResource(R.drawable.shape_create_btn_bg_false)
            }
            dataBinding.tvConfirm.isClickable = it
        }
    }

    private fun initClick() {
        dataBinding.tvConfirm.setOnClickListener {
            viewModel.getTransferInfo(coin, contractAddress, title, data)
        }
        dataBinding.include9.ivHeadBack.setOnClickListener { this.finish() }
        ClickUtils.applyGlobalDebouncing(dataBinding.include9.ivHeadFunction) {
            val intent = Intent(this, ScanningActivity::class.java)
            intent.putExtra("type", "transfer_address")
            startActivityForResult(intent, 0)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llSetPrice) {
            //设置矿工费

            BusUtils.postSticky(
                BusConfig.out_data_sticky_select_setting_mainer_fee_activity,
                viewModel.getGasEntity().value
            )

            val intent = Intent(this, SettingMinerFeeActivity::class.java)
            startActivityForResult(intent, 0)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.ivAddress) {
            val intent = Intent(this, AddressActivity::class.java)
            intent.putExtra("isSelect", true)
            startActivityForResult(intent, 0)
        }
    }

    private fun initView() {
        dataBinding.etAddress.hint = getString(R.string.coinAddress, coin?.toUpperCase())
        dataBinding.tvCoin.text = "0 $title"
        address?.let {
            viewModel.setAddressValue(it)
        }
        amount?.let {
            viewModel.setAmountValue(it)
        }
    }

    private fun initTitle() {
        dataBinding.include9.tvHeadTitles.text = getString(R.string.coinTransfer, title)
        dataBinding.include9.ivHeadFunction.visibility = View.VISIBLE
        dataBinding.include9.ivHeadFunction.setImageResource(R.mipmap.src_lib_eui_icon_scan)
        dataBinding.include9.clHeadHome.setBackgroundColor(ColorUtils.getColor(R.color.fragment_bg))
        dataBinding.include9.vLine.visibility = View.INVISIBLE
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 100) {
            val address = data?.getStringExtra("address")
            val amount = data?.getStringExtra("amount")
            dataBinding.etAddress.setText(address)
            dataBinding.etAddress.setSelection(address?.length ?: 0)
            amount?.let {
                dataBinding.etAmount.setText(it)
            }

        } else if (resultCode == 200) {
            //矿工费选择
            val position = data?.getIntExtra("position", 0) ?: 0
            viewModel.setStepSelect(position)
        }
    }

    override fun dialog(dialog: BottomSheetDialogFragment) {
        dialog.dismiss()
        BusUtils.postSticky(BusConfig.update_data_sticky_wallet_details_activity)
        this.finish()
    }
}